package com.CGFrame.struts.bean.detail;

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.Vector;


import com.CGFrame.struts.POJOs.Element;
import com.CGFrame.struts.POJOs.Elementgroup;
import com.CGFrame.struts.POJOs.Elementshot;
import com.CGFrame.struts.POJOs.Project;
import com.CGFrame.struts.POJOs.Shot;
import com.CGFrame.struts.POJOs.Shotgroup;

import com.CGFrame.struts.bean.ProjectOperate;
import com.CGFrame.struts.bean.ProjectOperateFactory;

public class DetailBean {
	
	public LinkedHashSet GroupElements(LinkedHashSet elements)
	{
		LinkedHashSet lhs = new LinkedHashSet();
		Vector v = new Vector();
		for(Iterator it = elements.iterator();it.hasNext();)
		{
			Element e = (Element)it.next();
			if(!Isin(v,e.getElementgroup().getElementGroupId()))
			{
				
				v.add(e.getElementgroup().getElementGroupId());
			}
		}
		for(int i=0;i<v.size();i++)
		{
			LinkedHashSet lhs1 = new LinkedHashSet();
			int groupId = (Integer)v.elementAt(i);
			for(Iterator it = elements.iterator();it.hasNext();)
			{
				Element element = (Element)it.next();
				if(element.getElementgroup().getElementGroupId()==groupId)
				{
					lhs1.add(element);
				}
			}
			lhs.add(lhs1);	
		}
		
		return lhs;
		
	}
	public LinkedHashSet GroupNames(LinkedHashSet elements)
	{
		LinkedHashSet lhs = new LinkedHashSet();
		Vector v = new Vector();
		for(Iterator it = elements.iterator();it.hasNext();)
		{
			Element e = (Element)it.next();
			if(!Isin(v,e.getElementgroup().getElementGroupId()))
			{
				v.add(e.getElementgroup().getElementGroupId());
				lhs.add(e.getElementgroup());
			}
		}
		return lhs;
	}
	public LinkedHashSet NoRelationElems(LinkedHashSet elements, String shotId)
	{
		LinkedHashSet lhs = new LinkedHashSet();
		Vector v = new Vector();
		for(Iterator it = elements.iterator();it.hasNext();)
		{
			Element e = (Element)it.next();
			if(!Isin(v,e.getElementgroup().getElementGroupId()))
			{
				v.add(e.getElementgroup().getElementGroupId());
				lhs.add(listElements(shotId,e.getElementgroup().getElementGroupId()+""));
			}
		}
		return lhs;
	}
	boolean Isin (Vector v,int n)
	{
		boolean res = false;
		if(v.size()!=0)
		{
			for(int i=0;i<v.size();i++)
			{
				if((Integer)v.elementAt(i)==n)
				{
					res = true;
				}
			}
		}
		return res;
		
	}
	
	public LinkedHashSet listElements (String ShotId,String GroupId)
	{
		LinkedHashSet lhs = new LinkedHashSet();
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		Elementgroup egp = (Elementgroup)pop.UniqueQuery("Elementgroup", "elementGroupId,"+GroupId);
		for(Iterator it = egp.getElements().iterator();it.hasNext();)
		{
			Element element = (Element)it.next();
			boolean temp = false;
			for(Iterator it1 = element.getElementshots().iterator();it1.hasNext();)
			{
				Elementshot es = (Elementshot)it1.next();
				if((es.getShot().getShotId()+"").equals(ShotId))
				{
					temp=true;
				}
			}
			if(temp==false) lhs.add(element);
		}
		return lhs;
		
	}
	public LinkedHashSet listAllElements(String projectId)
	{
		LinkedHashSet lhs = new LinkedHashSet();
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		Project project = (Project)pop.UniqueQuery("Project", "projectId,"+projectId);
		for(Iterator it = project.getElementgroups().iterator();it.hasNext();)
		{
			Elementgroup egp = (Elementgroup)it.next();
			if(!egp.getElements().isEmpty())
			{
				lhs.add(egp);
			}
		}
		return lhs;
	}
	public LinkedHashSet listGroups (String  projectId)
	{
		LinkedHashSet lhs = new LinkedHashSet();
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		Project project = (Project)pop.UniqueQuery("Project", "projectId,"+projectId);
		for(Iterator it = project.getElementgroups().iterator();it.hasNext();)
		{
			Elementgroup egp = (Elementgroup)it.next();
			if(!egp.getElements().isEmpty())
			{
				
				lhs.add(egp);
			}
		}
		return lhs;
		
	}
	public LinkedHashSet listElems (String  projectId, String shotId)
	{
		LinkedHashSet lhs = new LinkedHashSet();
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		Project project = (Project)pop.UniqueQuery("Project", "projectId,"+projectId);
		Shot shot = (Shot)pop.UniqueQuery("Shot", "shotId,"+shotId);
		for(Iterator it = project.getElementgroups().iterator();it.hasNext();)
		{
			Elementgroup egp = (Elementgroup)it.next();
			if(!egp.getElements().isEmpty())
			{
				LinkedHashSet lhs1 = new LinkedHashSet();
				for(Iterator it1 = egp.getElements().iterator();it1.hasNext();)
				{
					Element e = (Element)it1.next();
					for(Iterator it2 = e.getElementshots().iterator();it2.hasNext();)
					{
						Elementshot es = (Elementshot)it2.next();
						if(es.getShot().getShotId()==Integer.parseInt(shotId))
						{
							e.setFlag(true);
							break;
						}
						else e.setFlag(false);
					}
					lhs1.add(e);
				}
				lhs.add(lhs1);
			}
		}
		return lhs;	
	}
	public LinkedHashSet listSGP(String projectId,String elementId)
	{
		int e_id = Integer.parseInt(elementId);
		LinkedHashSet lhs = new LinkedHashSet();
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		Project project = (Project)pop.UniqueQuery("Project", "projectId,"+projectId);
		Element element = (Element)pop.UniqueQuery("Element", "elementId,"+elementId);
		for(Iterator it = project.getShotgroups().iterator();it.hasNext();)
		{
			int i=0;
			Shotgroup sgp = (Shotgroup)it.next();
			if(sgp.getShots().size()==0) continue;
			try{
				sgp.setShotGroupName( sgp.getShotgroup().getShotGroupName()+"_"+sgp.getShotGroupName() );				
			}
			catch(NullPointerException e)
			{
				
			}
			for(Iterator it1 = sgp.getShots().iterator();it1.hasNext();)
			{
				Shot shot = (Shot)it1.next();
				for(Iterator it2 = shot.getElementshots().iterator();it2.hasNext();)
				{
					Elementshot es = (Elementshot)it2.next();
					if(es.getElement().getElementId()==e_id)
					{
						i++;
						break;
					}
				}
			}
			if(i==sgp.getShots().size())
				sgp.setRelation_flag(2);
			else if(i!=0)
				sgp.setRelation_flag(1);
			else sgp.setRelation_flag(0);
			lhs.add(sgp);
		//	System.out.println("size="+sgp.getShots().size()+" i="+i);
		}
		return lhs;
	}
}
	